﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class logInModule : Form
    {
        private Account account;

        public logInModule()
        {
            account = new Account();
            InitializeComponent();
            this.userDropDown.SelectedIndex = 1;
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (passwordTextBox.Text.Equals(""))
            {
                MessageBox.Show("Password cannot be empty.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                String pass = account.getPassword(userDropDown.Text);
                if (!pass.Equals(""))
                {
                    if (passwordTextBox.Text.Equals(pass))
                    {
                        selection form = new selection(this);
                        form.Show();
                        if (userDropDown.Text == "Staff")
                        {
                            form.getGenerateReportSheet().Enabled = false;
                            form.getViewPreviousReportsButton().Enabled = false;
                            form.setPrivilege(0);
                        }
                        else
                        {
                            form.setPrivilege(1);
                        }
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("Incorrect password", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    passwordTextBox.Text = "";
                }
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            DialogResult result = MessageBox.Show("Are you sure you want to exit the program?", "Warning", MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (result == DialogResult.No)
                e.Cancel = true;
        }

        private void changePasswordButton_Click(object sender, EventArgs e)
        {
            if (passwordTextBox.Text.Equals(""))
            {
                MessageBox.Show("Enter the details of the account which you want to change the password.", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (passwordTextBox.Text.Equals(account.getPassword(userDropDown.Text)))
                {
                    changePassword changePass = new changePassword(this);
                    changePass.ShowDialog();
                    passwordTextBox.Text = "";
                }
                else
                {
                    MessageBox.Show("Incorrect password", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    passwordTextBox.Text = "";
                }
            }
        }

        public ComboBox getUserDropDown()
        {
            return userDropDown;
        }
    }
}
